package com.wealth.scsj.b_app.view.fragment.commentmodel;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.wealth.scsj.b_app.R;
import com.wealth.scsj.b_app.adapter.LeftCommAdapter;
import com.wealth.scsj.b_app.base.UserEvaluateBase;
import com.wealth.scsj.b_app.bean.GetEvaluationNumBean;
import com.wealth.scsj.b_app.bean.UserEvaluateListBean;
import com.wealth.scsj.b_app.bean.UserRequestEvaluateBean;
import com.wealth.scsj.b_app.presenter.UserEvaluatePresenter;
import com.wealth.scsj.b_app.view.fragment.basemodel.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import okhttp3.RequestBody;

/**
 * Created by Administrator on 2018/7/26.
 */

public class AllCommentFragment extends BaseFragment implements UserEvaluateBase.IView, View.OnClickListener {
    private RecyclerView mRecyclerView;
    private UserEvaluatePresenter userEvaluatePresenter;
    private List<UserEvaluateListBean.DataBean> leftCommBeanList;
    private LeftCommAdapter adapter;

    private RelativeLayout allEvaluation, goodEvaluation, badEvaluation, midEvaluation;
    private TextView allEvaluationTv, goodEvaluationTv, badEvaluationTv, midEvaluationTv;

    private int type = 0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_left_comm, null);
        initView(view, null);
        initListener();
        initData();
        return view;
    }


    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        mRecyclerView = view.findViewById(R.id.comment_list_rv);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getHoldingActivity()));
        userEvaluatePresenter = new UserEvaluatePresenter(this);

        allEvaluation = view.findViewById(R.id.comment_all_rl);
        goodEvaluation = view.findViewById(R.id.comment_good_rl);
        badEvaluation = view.findViewById(R.id.comment_bad_rl);
        midEvaluation = view.findViewById(R.id.comment_mid_rl);

        allEvaluationTv = view.findViewById(R.id.comment_all_tv);
        goodEvaluationTv = view.findViewById(R.id.comment_good_tv);
        badEvaluationTv = view.findViewById(R.id.comment_bad_tv);
        midEvaluationTv = view.findViewById(R.id.comment_mid_tv);
    }

    @Override
    protected void initListener() {
        allEvaluation.setOnClickListener(this);
        goodEvaluation.setOnClickListener(this);
        badEvaluation.setOnClickListener(this);
        midEvaluation.setOnClickListener(this);

    }

    @Override
    protected void initData() {
        getData();

        //获取评价个数
        getEvaluationNum();
    }

    private void getEvaluationNum() {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("storeId", "c1b9d0ec-749d-11e8-afb7-70106fb15682");
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonObject.toString());
        userEvaluatePresenter.deliverEvaluationNumData(body);
    }

    @Override
    protected int getLayoutId() {
        return 0;
    }

    @Override
    public void showSuccessMsg(String s, UserEvaluateListBean userEvaluateListBean) {
        leftCommBeanList = userEvaluateListBean.getData();
        adapter = new LeftCommAdapter(getHoldingActivity(), leftCommBeanList);
        mRecyclerView.setAdapter(adapter);
    }

    @Override
    public void showEvaluationNumSuccessMsg(String s, GetEvaluationNumBean getEvaluationNumBean) {
        //获取评价个数成功
        //全部
        int allEvaluation= getEvaluationNumBean.getData().getTotalEvaluateNumber();
        //好评
        int goodEvaluation= getEvaluationNumBean.getData().getGoodEvaluateNumber();
        //差评
        int badEvaluation= getEvaluationNumBean.getData().getBadEvaluateNumber();
        //一般
        int midEvaluation= getEvaluationNumBean.getData().getGeneralEvaluation();
        allEvaluationTv.setText("全部 "+allEvaluation);
        goodEvaluationTv.setText("好评 "+goodEvaluation);
        badEvaluationTv.setText("差评 "+badEvaluation);
        midEvaluationTv.setText("一般 "+midEvaluation);

    }

    @Override
    public void showErrorMsg(String s) {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.comment_all_rl:
                allEvaluationTv.setBackgroundResource(R.mipmap.rectage_img);
                goodEvaluationTv.setBackgroundResource(R.drawable.mark_selected_shape9);
                badEvaluationTv.setBackgroundResource(R.drawable.mark_selected_shape9);
                midEvaluationTv.setBackgroundResource(R.drawable.mark_selected_shape9);

                allEvaluationTv.setTextColor(Color.WHITE);
                goodEvaluationTv.setTextColor(Color.BLACK);
                badEvaluationTv.setTextColor(Color.BLACK);
                midEvaluationTv.setTextColor(Color.BLACK);

                type = 0;
                getData();

                break;
            case R.id.comment_good_rl:
                allEvaluationTv.setBackgroundResource(R.drawable.mark_selected_shape9);
                goodEvaluationTv.setBackgroundResource(R.mipmap.rectage_img);
                badEvaluationTv.setBackgroundResource(R.drawable.mark_selected_shape9);
                midEvaluationTv.setBackgroundResource(R.drawable.mark_selected_shape9);

                allEvaluationTv.setTextColor(Color.BLACK);
                goodEvaluationTv.setTextColor(Color.WHITE);
                badEvaluationTv.setTextColor(Color.BLACK);
                midEvaluationTv.setTextColor(Color.BLACK);
                type = 5;
                getData();
                break;
            case R.id.comment_bad_rl:
                allEvaluationTv.setBackgroundResource(R.drawable.mark_selected_shape9);
                goodEvaluationTv.setBackgroundResource(R.drawable.mark_selected_shape9);
                badEvaluationTv.setBackgroundResource(R.mipmap.rectage_img);
                midEvaluationTv.setBackgroundResource(R.drawable.mark_selected_shape9);

                allEvaluationTv.setTextColor(Color.BLACK);
                goodEvaluationTv.setTextColor(Color.BLACK);
                badEvaluationTv.setTextColor(Color.WHITE);
                midEvaluationTv.setTextColor(Color.BLACK);
                type = 3;
                getData();
                break;
            case R.id.comment_mid_rl:
                allEvaluationTv.setBackgroundResource(R.drawable.mark_selected_shape9);
                goodEvaluationTv.setBackgroundResource(R.drawable.mark_selected_shape9);
                badEvaluationTv.setBackgroundResource(R.drawable.mark_selected_shape9);
                midEvaluationTv.setBackgroundResource(R.mipmap.rectage_img);

                allEvaluationTv.setTextColor(Color.BLACK);
                goodEvaluationTv.setTextColor(Color.BLACK);
                badEvaluationTv.setTextColor(Color.BLACK);
                midEvaluationTv.setTextColor(Color.WHITE);
                type = 4;
                getData();
                break;
            default:
                break;

        }
    }

    /**
     * 请求数据
     */

    private void getData() {
        UserRequestEvaluateBean userRequestEvaluateBean = new UserRequestEvaluateBean();
        userRequestEvaluateBean.setStoreId("c1b9d0ec-749d-11e8-afb7-70106fb15682");
        userRequestEvaluateBean.setType(type);
        userRequestEvaluateBean.setHeightFrameCapture(60);
        userRequestEvaluateBean.setWidthFrameCapture(60);
        UserRequestEvaluateBean.PageInfoBean pageInfoBean = new UserRequestEvaluateBean.PageInfoBean();
        pageInfoBean.setPageNum(10);
        pageInfoBean.setPageSize(1);
        Gson gson = new Gson();
        String obj = gson.toJson(userRequestEvaluateBean);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), obj);
        userEvaluatePresenter.deliverData(body);
    }
}
